package demo1;

import java.util.Arrays;

public class MyArrayList{
    private int[] elem ;
    private int usedSize ;
    private static final int DEFAULT_CAPACITY = 2 ;
    public MyArrayList(){
        this.elem = new int[DEFAULT_CAPACITY] ;
    }
    public MyArrayList(int defaultCapacity){
        this.elem = new int[defaultCapacity] ;
    }
    public void disPlay(){
        for(int i =0  ; i < this.usedSize ; i++){
            System.out.print(this.elem[i] + "  ");
        }
        System.out.println();
    }
    public void add (int data){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem , 2* this.elem.length) ;
            }
        this.elem[this.usedSize] = data ;
        this.usedSize++ ;
    }
    public void add(int pos , int data){
        if(pos <0 || pos > this.usedSize){
           throw new PosOutBounds(pos + "  pos不合法") ;
        }
        for(int i = this.usedSize - 1 ; i >= pos ; i--){
            this.elem[i+1] = this.elem[i] ;
        }
        this.elem[pos] = data ;
        this.usedSize ++ ;

    }
    public boolean contains(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return true ;
            }
        }
        return false ;
    }
    public int indexOf(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return i ;
            }
        }
        return -1 ;
    }
    public int get(int pos){
        checkPos(pos);
        return this.elem[pos] ;
    }
    public void set(int pos ,int data){
        checkPos(pos);
        this.elem[pos] = data ;
    }

    public boolean isFull(){
        if(this.usedSize == this.elem.length){
            return true ;
        }
        return false ;
    }
    public void checkPos(int pos){
        if(pos < 0 || pos >= this.usedSize){
            throw new PosOutBounds(pos + "  pos不合法");
        }
    }
    public int size(){
        return this.usedSize ;
    }
    public void clear(){
     this.usedSize = 0 ;
     }
    public void remove(int toRemove){
        int ret = indexOf(toRemove) ;
        if(ret == -1){
            System.out.println("顺序表里没有这个数据");
             return;
        }
        for (int i = ret ; i < this.usedSize- 1 ; i++){
            this.elem[i] = this.elem[i+ 1] ;
        }
        this.usedSize-- ;
    }


}